﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.ComponentModel.Design;

[assembly: WebResource("AGuang.Script.AGuang_Client.Controls.Calendar.js", "text/javascript")]
[assembly: WebResource("AGuang.Script.AGuang_Client.css.Calendar.css", "text/css",PerformSubstitution=true)]
[assembly: WebResource("AGuang.Script.AGuang_Client.css.images.Calendar.left.gif", "image/gif")]
[assembly: WebResource("AGuang.Script.AGuang_Client.css.images.Calendar.right.gif", "image/gif")]
namespace AGuang.Script {
    /// <summary>
    /// 日期选择控件
    /// </summary>
    public class Calendar:BaseScript {
        private String forid;
        private String forClientControlID;
        private String dateTimeFormat=null;
        private bool autoScript=false;
        private String cssClass;

        private String onDayChange;
        private String onMonthChange;
        private String onYearChange;

        protected override void OnLoad(EventArgs e) {
            //设为兼容IE7<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
            bool hasMate = false;
            foreach (Control oc in Page.Header.Controls) {
                if (oc is HtmlMeta) {
                    HtmlMeta hgc = (HtmlMeta)oc;
                    if (hgc.Content.ToLower() == "IE=EmulateIE7".ToLower()) {
                        hasMate = true;
                    }
                }
            }
            if (!hasMate) {
                HtmlMeta m = new HtmlMeta();
                m.HttpEquiv = "X-UA-Compatible";
                m.Content = "IE=EmulateIE7";
                Page.Header.Controls.AddAt(0,m);
            }
            ClientScriptManager csm = Page.ClientScript;
            AddStyle(csm.GetWebResourceUrl(this.GetType(), "AGuang.Script.AGuang_Client.css.Calendar.css"));
            base.OnLoad(e);
            AddScriptContorl(new Date());
            AddScriptContorl(new Action());
            AddScript(csm.GetWebResourceUrl(this.GetType(), "AGuang.Script.AGuang_Client.Controls.Calendar.js"));
        }

        protected override void OnPreRender(EventArgs e) {
            base.OnPreRender(e);
            if (!AutoScript) return;
            if (String.IsNullOrEmpty(forid)) throw new Exception("请指定“服务器控件编号”或“客户端控件编号”");
            Control c = this.Parent.FindControl(forid);
            if (c != null) { forid = c.ClientID; }
            StringBuilder sb = new StringBuilder();
            if (String.IsNullOrEmpty(this.DateTimeFormat)) {
                sb.AppendFormat("var {0}=new Page.Calendar('{1}');", JsVar, forid);
            } else {
                sb.AppendFormat("var {0}=new Page.Calendar('{1}','{2}');", JsVar, forid, this.DateTimeFormat);
            }
            if (!String.IsNullOrEmpty(CssClass)) {
                sb.AppendFormat("{0}.SetCssClass('{1}');", JsVar, CssClass);
            }
            if (!String.IsNullOrEmpty(OnDayChange)) {
                sb.AppendFormat("{0}.Add_DayChange({1});", JsVar, OnDayChange);
            }
            if (!String.IsNullOrEmpty(onMonthChange)) {
                sb.AppendFormat("{0}.Add_MonthChange({1});", JsVar, onMonthChange);
            }
            if (!String.IsNullOrEmpty(onYearChange)) {
                sb.AppendFormat("{0}.Add_YearChange({1});", JsVar, onYearChange);
            }
            AddEffect(sb.ToString());
            
        }
        /// <summary>
        /// 服务器控件编号
        /// </summary>
        [TypeConverter(typeof(ControlIDConverter))]
        public String ForServerControlID {
            get { return forid; }
            set { forid = value; }
        }

        /// <summary>
        /// 客户端控件编号
        /// </summary>
        [TypeConverter(typeof(ControlIDConverter))]
        public String ForClientControlID {
            get { return forid; }
            set { forid = value; }
        }

        /// <summary>
        /// 日期格式
        /// </summary>
        public String DateTimeFormat {
            get { return dateTimeFormat; }
            set { dateTimeFormat = value; }
        }

        /// <summary>
        /// 是否自动生成脚本
        /// </summary>
        public bool AutoScript {
            get { return autoScript;}
            set { autoScript=true;}
        }

        /// <summary>
        /// 设置日历的样式
        /// </summary>
        public String CssClass {
            get { return cssClass; }
            set { cssClass = value; }
        }

        public String OnDayChange {
            get { return onDayChange; }
            set { onDayChange=value; }
        }

        public String OnMonthChange {
            get { return onMonthChange; }
            set { onMonthChange=value; }
        }

        public String OnYearChange {
            get { return onYearChange; }
            set { onYearChange = value; }
        }

        public String JsVar {
            get { return "s" + this.ClientID; }
        }
    }
}
